Hello and welcome back to this session about user defined functions.
So whenever we want to reuse or repeat the same or very similar code more than once it may be worth
writing a reusable function.
And by doing so this can help us to make our code more readable.
Functions are nothing new.
And we use a lot of built in functions so far but.
First of all let's create a list ATL containing four integers 3 2 5 6 and we already know the function
the built in function print so we can print our list and for example there's also the function sorted
that we can start our list and then ascending minor so here we have 2 3 5 6 and saw it actually creates
a sorted copy off.
So if we want to really sort our list out we have to reassign l.
So now let's print again.
Oh and here we have these sorted lists L and we already know methods which are functions that are attached
to a specific objects for instance we know that the list object has and the method append where we can
append another element to the end of the list.
So for example we can here append the element 7 to the end of the list and let's print out our list
again and there we have 4 7 at the end of the list.
And as a general rule functions work for different objects so you can print out a list or an integer
on float with the function print.
And in contrast to that methods are functions specifically designed for particular objects.
So for example here append the append method is a particular function for.
For the list object.
All right.
And we already know more functions so we know the lengths function which calculates them the amount
of elements in our list so we have five elements and we already know the main function which calculates
that the minimum element of our list.
So it s 2 and we also know the max function
and we've already seen also the SUM function which takes them the sum over all elements in our list.
Now we have seen that the output and functions like minimal maximum sum and one could think that now
there should be also a mean function which calculates them the mean of our list.
And if we try to apply that mean function to our list L and then Python to assess them that then I mean
is not defined.
So there is no need function and now we can create our own user defined mean function that calculates
them the mean of.
Mm.
Yeah.
List and before we start creating the function I mean we should first of all think about how to calculate
the mean of a sequence of numbers and the.
Actually we just have to take them the sum of them all elements divided by the amount of elements itself.
So let's check what we get here.
So we have here the sum is 23 and the length is five.
So twenty three divided by five gives us four point six.
And now what we can do we can yeah define our new function mean and yeah we have to start with the keyword
death.
Here you see it in green it's a keyword and then we say mean.
So our function so to have the name mean and then within the brackets we define a parameter x so x could
stand for a list or another type of sequence like a tuple or so on and then we have to make a call on
and then if we press and uh we see that the python automatically creates here and then.
So this is called the function header that we defined the function.
And now here is the function body where we define what are the functions of actually calculate.
And here we have to make an indent so the function should calculate the sum over our parameter x divided
by the length of our parameter X then we can delete here the empty line and then we have to define what
our function so to actually return.
So here in the function body we define what the functions calculate and then in the end we should define
what our function should return.
So we use them the keyword return and the function of such return us them they mean.
So now we can run the cell
and here once again.
So this is some of the same quote here if you compare it and then what we can do we can call the function
bypassing the argument added to the function.
So Ellis our list and the function expects an argument for the parameter x.
So our argument is then the list L..
And let's see what we get here.
So here we get four point six and we can also do this more explicitly so we can call the function by
saying okay our parameter X should be our list ello.
So this is actually the same as above Yeah.
And our function should not only work for lists but have also for other sequences like tuple so we can
define that tuple t with the elements for five six seven and then we should also be able to calculate
them the mean of our total because if you look at the function body here.
So we calculate the sum of a sequence divided by the length of a sequence and some in length.
Functions should also work for tuples so our function our user defined function means would also work
for us.
Let's see if it works and here we are and what I also want to show you.
So there's no need to define yeah a variety of mean we can directly say okay.
Please return me the sum of our sequence x divided by the length of our sequence X so this would also
work
and let's run it again and there can see it so it also works.
All right so let's move to another example and we assign the integer seven to our arrival a
and then we can calculate a squared.
So it s forty nine yeah.
Then if you want to change a.
So let's say we want to change a two A's then we can rerun the cells and get sixty far and whenever
we want to reuse a code we can also define a function so we can define the function square with them
the parameter X and the function so to return us them the square of our parameter X
so let's try for and call the function by passing the argument for
and the function gives the sixteen and the same as above.
We can also make it more explicitly by calling the function saying that the parameter X should be for
so this is the same but actually what we have to do we have to pass the an argument to our function.
So if we just call our function with no argument we get an error message.
So you can see is our function squares missing 1 require positional argument x.
However what we can do we can set a default value to our paramedics so we can define our functions create
two by saying that the default value of our parameter X should be to and the function so return x cared.
So now if you call the function without any argument the function automatically applies.
Our default value too.
So now we would expect here therefore as a return of the function and correct of course the function
can also be called the new argument.
So for example for gives us 16 and same as above we can make this more explicitly by saying our parameter
X is for.
And this also works.
All right.
These were the basics of youth that defined functions and the next two sat sessions we are going a bit
more into detail so hope to see you there by.